@extends('layouts.app')
@include('user.sidebar')
@section('content')
    <div class="content">
        <div class="row custom-invoice">
            <div>
                <form class="form form-inline" method="get" action="{{ route('admin.user.staff') }}">
                    <div class="form-group">
                        <input class="form-control" type="text" name="search" id="" value="{{ $search }}" placeholder="电话/昵称/姓名">
                    </div>
                    <div class="form-group">
                        <input class="form-control" type="text" name="searchShop" id="" value="{{ $searchShop }}" placeholder="店铺">
                    </div>
                    <button type="submit" class="btn btn-success" value="">搜索</button>
                    <button type="submit" class="btn btn-primary" name="export" value="1">导出</button>
                </form>
            </div>
            <br />
            <span>数量：{{ $count }} </span>
            <table class="table table-striped table-hover">
                <thead>
                <tr>
                    <th>真实姓名</th>
                    <th>用户昵称</th>
                    <th>用户头像</th>
                    <th>电话号</th>
                    <th>所属商户</th>
                    <th>级别</th>
                    <th>职位</th>
                    <th>邀请码</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                @foreach($data as $item)
                    <tr>
                        <td>{{ $item->name }}</td>
                        <td><a href="{{ route('admin.order.good_index', ['referrer_id' => $item->id]) }}" target="_blank">{{ $item->nickname }}</a></td>
                        <td><img src="{{ $item->avatar }}" width="30"></td>
                        <td>{{ $item->mobile }}</td>
                        <td>
                            @if(!empty($item->shop_id))
                                @php
                                    $shopName = "";
                                    if (!empty($item->shop_id)) {
                                        foreach ($item->shop_id as $value) {
                                            $shop = $allShops->get($value);
                                            if (!is_null($shop)) {
                                                $shopName .= $shop->name."<br />";
                                            }
                                        }
                                    }
                                @endphp
                                {!! $shopName !!}
                            @endif
                        </td>
                        <td>{{ $item->employee_identity }}</td>
                        <td>{{ $item->position }}</td>
                        <th>{{ $item->invite_code }}</th>
                        <td>
                            @can("recharge_record_recharge")
                                <a class="btn btn-primary btn-xs" href="{{ route('admin.rechargeRecord.recharge',['user_id' => $item->id]) }}"><i class="fa fa-edit"></i> 充值</a>
                            @endcan
                            @can('user-level3-edit')
                            <a class="btn btn-primary btn-xs" href="{{ route('admin.user.staff.edit', ['id' => $item->id]) }}"><i class="fa fa-eidt"></i> 修改</a>
                            @endcan
                            @can('user-level3-level')
                            <a class="btn btn-primary btn-xs changeLevel" data-url="{{ route('admin.user.level', ['id' => $item->id]) }}" data-level="{{ $item->level }}" data-management="{{ $item->is_palm_management }}"><i class="fa fa-user"></i> 修改身份</a>
                            @endcan
                        </td>
                    </tr>
                @endforeach
                </tbody>
            </table>
            {{ $data->appends(['search' => $search, 'searchShop' => $searchShop])->links() }}
        </div>
        <div class="modal fade loading custom-hide" id="hideForm" tabindex="-1" role="dialog" aria-labelledby="hideFormLibel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
            <div class="modal-dialog">
                <div class="content invoice" style="text-align: left">
                    <form class='form'  action="" method="POST">
                        <input type="hidden" name="_token" value="{{ csrf_token() }}" />
                        <div class="form-group">
                            <label for="level">身份：</label>
                            <select class="form-control" name="level" id="level">
                                @foreach(\App\Modules\User\Models\User::$levelShow as $key => $value)
                                    <option value="{{ $key }}">{{ $value }}</option>
                                @endforeach
                            </select>
                        </div>
                        <!-- <div class="form-group">
                            <label for="identity">是否开通掌上管理权限：</label>
                            <select class="form-control" name="is_palm_management" id="is_palm_management">
                                <option value="0">否</option>
                                <option value="1">是</option>
                            </select>
                        </div> -->
    {{--                    <div class="form-group hidden" id="identity-div">--}}
    {{--                        <label for="identity">选择职位：</label>--}}
    {{--                        <select class="form-control" name="identity" id="identity">--}}
    {{--                            <option value="{{ \App\Modules\User\Models\User::EMPLOYEE_IDENTITY_PRIMARY }}">{{ \App\Modules\User\Models\User::EMPLOYEE_IDENTITY_PRIMARY }}</option>--}}
    {{--                            <option value="{{ \App\Modules\User\Models\User::EMPLOYEE_IDENTITY_MIDDEL }}">{{ \App\Modules\User\Models\User::EMPLOYEE_IDENTITY_MIDDEL }}</option>--}}
    {{--                            <option value="{{ \App\Modules\User\Models\User::EMPLOYEE_IDENTITY_SENIOR }}">{{ \App\Modules\User\Models\User::EMPLOYEE_IDENTITY_SENIOR }}</option>--}}
    {{--                        </select>--}}
    {{--                    </div>--}}
                        <div class="form-group">
                            <button type="submit" class="btn btn-success form-control">提交</button>
                            <br />
                            <br />
                            <button type="button" class="btn" id="closeHideForm">关闭</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
@endsection

@push('scripts')
    <script>
        $('.changeLevel').on('click', function () {
            $("#hideForm form").attr('action', $(this).attr('data-url'));
            var level = $(this).attr('data-level');
            // if (level == 3) {
            //     $('#identity-div').removeClass('hidden');
            // }
            var management = $(this).attr('data-management');

            $("#level").find("option[value='"+ level +"']").attr('selected', true);
            $("#is_palm_management").find("option[value='"+ management +"']").attr('selected', true);

            $("#hideForm").modal('show');
        });
        $('#closeHideForm').on('click', function () {
            $("#hideForm").modal('hide');
        });

        // $('#level').on('click', function () {
        //     if ($(this).val() == 3) {
        //         $('#identity-div').removeClass('hidden');
        //     } else {
        //         $('#identity-div').addClass('hidden');
        //     }
        // });

        $('#update-referrer').select2({
            ajax: {
                dataType: 'json',
                delay: 250,
                url: function () {
                    return '{{ route('admin.search_user') }}';
                },
                processResults: function (data) {
                    return {
                        results: $.map(data, function(row, key) {
                            return {
                                id: key,
                                text: row
                            };
                        })
                    };
                }
            }
        });
    </script>
@endpush
